import { Ref, ref } from 'vue'
import { ElMessage } from 'element-plus'

export interface SelectedRow {
    id?: number
    is_present: number
    is_wait: number
    discount: number
    isUseSingleFoodDiscount: boolean
    making_ids: number[]
    write_making: any[]
    food_sku_id: number
    food_sku_name: string
    food_sku_price: number
    food_sku_unit: string
    food_sku_num: number
    foodName?: string
    foodGroupName?: string
    foodUnit?: string
    foodNum: number
    foodPrice: number
    foodTotalPrice: number
    foodDescName?: string
    print?: number
    [key: string]: any
}

export function useSingleFoodActions(options: {
    selectedListState: Ref<SelectedRow[]>
    activeIndexState: Ref<number>
    activeCountState: Ref<number>
}) {
    const { selectedListState, activeIndexState } = options

    // 单个菜品等叫弹框状态
    const showSingleWaitDialog = ref(false)

    // 单个菜品赠送弹框状态
    const showSingleGiftDialog = ref(false)

    // 单个菜品打折弹框状态
    const showSingleDiscountDialog = ref(false)

    // 打开单个菜品等叫弹框
    const handleShowSingleWait = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) {
            ElMessage.warning('请先选择菜品')
            return
        }
        showSingleWaitDialog.value = true
    }

    // 打开单个菜品赠送弹框
    const handleShowSingleGift = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) {
            ElMessage.warning('请先选择菜品')
            return
        }
        showSingleGiftDialog.value = true
    }

    // 打开单个菜品打折弹框
    const handleShowSingleDiscount = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) {
            ElMessage.warning('请先选择菜品')
            return
        }
        showSingleDiscountDialog.value = true
    }

    // 切换单个菜品打印状态
    const handleToggleSinglePrint = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) {
            ElMessage.warning('请先选择菜品')
            return
        }
        const currentItem = selectedListState.value[activeIndexState.value]
        // 切换打印状态
        currentItem.print = currentItem.print === 1 ? 0 : 1
        ElMessage.success(`已设置为${currentItem.print === 1 ? '打印' : '不打印'}`)
    }

    // 单个菜品等叫确认
    const onSingleWaitConfirm = (payload: { printType: number; waitTime?: string }) => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) return
        const currentItem = selectedListState.value[activeIndexState.value]
        currentItem.is_wait = 1 // 设置为等叫
        currentItem.print = payload.printType // 设置打印状态
        currentItem.status = 3 // 设置为等叫
        if (payload.waitTime) {
            currentItem.wait_time = payload.waitTime // 设置等叫时间
        }
        showSingleWaitDialog.value = false
        ElMessage.success('已设置为等叫')
    }

    // 单个菜品赠送确认
    const onSingleGiftConfirm = (payload: { reason: string; num: number }) => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) return
        const currentItem = selectedListState.value[activeIndexState.value]
        currentItem.is_present = 1 // 设置为赠送
        currentItem.gift_reason = payload.reason // 设置赠送原因
        currentItem.present_num = payload.num // 设置赠送数量
        showSingleGiftDialog.value = false
        ElMessage.success('已设置为赠送')
    }

    // 单个菜品撤赠确认
    const onSingleRevokeGiftConfirm = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) return
        const currentItem = selectedListState.value[activeIndexState.value]
        currentItem.is_present = 0 // 撤销赠送状态
        delete currentItem.gift_reason // 清除赠送原因
        currentItem.present_num = 0 // 清除赠送数量
        showSingleGiftDialog.value = false
        ElMessage.success('已撤销赠送')
    }

    // 单个菜品打折确认
    const onSingleDiscountConfirm = (payload: { discount: string }) => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) return
        const currentItem = selectedListState.value[activeIndexState.value]
        currentItem.discount = Number(payload.discount) // 设置折扣值
        currentItem.isUseSingleFoodDiscount = true // 标记为单品打折
        // 重新计算总价
        const base = Number(currentItem.foodPrice || 0)
        const discountRate = Number(currentItem.discount || 10) / 10
        currentItem.foodTotalPrice = Number((Number(currentItem.foodNum || 0) * base * discountRate).toFixed(2))
        showSingleDiscountDialog.value = false
        ElMessage.success('已设置单品打折')
    }

    // 单个菜品撤销打折确认
    const onSingleRevokeDiscountConfirm = () => {
        if (activeIndexState.value === -1 || activeIndexState.value >= selectedListState.value.length) return
        const currentItem = selectedListState.value[activeIndexState.value]
        currentItem.discount = 10 // 恢复原价
        currentItem.isUseSingleFoodDiscount = false
        // 重新计算总价
        const base = Number(currentItem.foodPrice || 0)
        currentItem.foodTotalPrice = Number((Number(currentItem.foodNum || 0) * base).toFixed(2))
        showSingleDiscountDialog.value = false
        ElMessage.success('已撤销单品打折')
    }

    return {
        // 弹框状态
        showSingleWaitDialog,
        showSingleGiftDialog,
        showSingleDiscountDialog,
        // 操作函数
        handleShowSingleWait,
        handleShowSingleGift,
        handleShowSingleDiscount,
        handleToggleSinglePrint,
        // 确认函数
        onSingleWaitConfirm,
        onSingleGiftConfirm,
        onSingleRevokeGiftConfirm,
        onSingleDiscountConfirm,
        onSingleRevokeDiscountConfirm
    }
}
